package LeetCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LC_530_MinimumAbsoluteDifferenceinBST {

    public static void main(String[] args) {

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public class Solution {
        List<Integer> nodes = new ArrayList<>();

        public int getMinimumDifference(TreeNode root) {
            triversal(root);
            Collections.sort(nodes);
            int result = Integer.MAX_VALUE;
            for (int i = 1; i < nodes.size(); i++)
                if (nodes.get(i) - nodes.get(i - 1) < result)
                    result = nodes.get(i) - nodes.get(i - 1);
            return result;
        }

        void triversal(TreeNode root) {
            if (root != null) {
                nodes.add(root.val);
                triversal(root.left);
                triversal(root.right);
            }
        }

    }
}
